package cn.iutils.code.main;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import cn.iutils.code.config.Config;
import cn.iutils.code.entity.TableModel;
import cn.iutils.code.entity.TreeNode;
import cn.iutils.code.service.CodeService;
import cn.iutils.code.service.DbService;
import cn.iutils.code.utils.FileUtils;
import cn.iutils.code.utils.db.DBManger;
import cn.iutils.code.utils.db.DBMangerPool;
public class Ajax{
	DbService dbService = new DbService();
	CodeService codeService = new CodeService();
	
	// 定义参数
	private static String dbUrl = null;// 数据库地址
	private static String usernameParam;// 数据库帐号
	private static String pwdParam;// 数据库密码
	private static String defPackage="";
	private static String defFilter="";
	public String getDefPackage() {
		return defPackage;
	}
	public String getDefFilter() {
		return defFilter;
	}
	
	public String getOutZipPath() {
		return Config.outCodePath+java.io.File.separator+Config.outCodePath+".zip";
	}
	public String getRootPath() {
		return FileUtils.getRootPath();
	}
	public Ajax() {
		if (dbUrl == null) {// 从配置文件里面读取
			String configUrl =FileUtils.getClassPath()+"config.properties";
			Properties properties = new Properties();
			InputStream input = null;
			try {
				input = new FileInputStream(configUrl);// 加载Java项目根路径下的配置文件
				properties.load(input);// 加载属性文件
				dbUrl = properties.getProperty("jdbc.url");
				usernameParam = properties.getProperty("jdbc.username");
				pwdParam = properties.getProperty("jdbc.password");
				defPackage= properties.getProperty("def.package");
				defFilter= properties.getProperty("def.filter");
			} catch (IOException io) {
			} finally {
				if (input != null) {
					try {
						input.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public List<String> getDatabase() {
		DBManger dBManger = new DBManger(dbUrl, usernameParam, pwdParam);
		DBMangerPool dBMangerPool = DBMangerPool.getInstance();
		dBMangerPool.setDbMangerPools(Config.dbKey, dBManger);
		List<String> dbs = dbService.getDatabase(Config.dbKey);		
		return dbs;
	}
	
	public List<TableModel> getTables(String dbName) {
		DBManger dBManger = new DBManger(dbUrl, usernameParam, pwdParam);
		DBMangerPool dBMangerPool = DBMangerPool.getInstance();
		dBMangerPool.setDbMangerPools(Config.dbKey, dBManger);
		List<TableModel> tables = dbService.getTables(Config.dbKey, dbName);
		return tables;
	}
	
	public boolean genCode(String charFilterParam,String myPackageParam,String myModelParam,String mySubModelParam,String treeNodes) {
		DBManger dBManger = new DBManger(dbUrl, usernameParam, pwdParam);
		DBMangerPool dBMangerPool = DBMangerPool.getInstance();
		dBMangerPool.setDbMangerPools(Config.dbKey, dBManger);
		if(mySubModelParam==null || "".equals(mySubModelParam)){
            mySubModelParam = null;
        }
		return codeService.auotoCode(TreeNode.getNodeFromString(treeNodes),charFilterParam,myPackageParam,myModelParam,mySubModelParam);
	}
}
